Completed
Push — master ( 7c2706...6e6b11 )
by D
02:19
created

Lightbox.bodyClose   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
dl 0
loc 17
rs 9.6
c 0
b 0
f 0
1
import {define} from "../globals/globals";
2
import {LightboxInterface} from "../interfaces/LightboxInterface";
3
import {LightboxOptions} from "../types/LightboxOptions";
4
import {ImagePositionOptions} from "../types/internal/ImagePositionOptions";
5
6
(function (root, factory) {
7
    if (typeof define === 'function' && define.amd) {
8
        define([], factory);
9
    } else if (typeof module === 'object' && module.exports) {
10
        module.exports = factory();
11
    } else {
12
        root.Lightbox = factory();
13
    }
14
}(typeof self !== 'undefined' ? self : this, function () {
15
16
    class Lightbox implements LightboxInterface {
17
        public options: LightboxOptions;
18
19
        constructor(options: LightboxOptions) {
20
21
            if (!options.targets) {
22
                throw new Error('No targets')
23
            }
24
25
            this.options = options;
26
            this.options.targets = typeof options.targets === 'string' ? document.querySelectorAll(options.targets) : options.targets;
27
            this.initClasses();
28
            this.initClone();
29
            this.bodyClose();
30
        }
31
32
        private initClasses(): void {
33
            this.options.targets.forEach(function (target: HTMLImageElement) {
34
                target.classList.add('lightbox');
35
            });
36
        }
37
38
        private initHTML(target: HTMLImageElement, position: ImagePositionOptions): void {
39
            let wrapper = document.createElement('div');
40
            wrapper.classList.add('lightbox-clone-wrapper');
41
42
            setTimeout(() => {
43
                wrapper.classList.add('active');
44
            }, 100);
45
46
            let clone = document.createElement('div');
47
            clone.classList.add('lightbox-clone');
48
            clone.style.top = position.top + 'px';
49
            clone.style.left = position.left + 'px';
50
            clone.style.width = target.naturalWidth + 'px';
51
52
            setTimeout(() => {
53
                clone.classList.add('centered');
54
            }, 100);
55
56
            let cloneImg = document.createElement('img');
57
            cloneImg.src = target.src;
58
59
            clone.appendChild(cloneImg);
60
            wrapper.appendChild(clone);
61
            document.body.appendChild(wrapper);
62
        }
63
64
        private initClone(): void {
65
            let that = this;
66
67
            this.options.targets.forEach(function (target: HTMLImageElement) {
68
                target.addEventListener('click', function (e) {
69
                    that.initHTML(e.target as HTMLImageElement, Lightbox.getTargetPosition(target));
70
71
                    that.scrollClose(target);
72
                })
73
            });
74
        }
75
76
        static getTargetPosition(target: HTMLImageElement): ImagePositionOptions {
77
            let targetPosition = target.getBoundingClientRect();
78
79
            return {
80
                top: targetPosition.top,
81
                left: targetPosition.left
82
            }
83
        }
84
85
        protected bodyClose(): void {
86
            let that = this;
87
88
            document.addEventListener('click', function (e) {
89
                e.preventDefault();
90
91
                if((e.target as HTMLElement).closest('.lightbox-clone-wrapper')) {
92
                    let wrapper = document.querySelector('.lightbox-clone-wrapper');
93
94
                    if(wrapper) {
95
                        let clone = wrapper.querySelector('.lightbox-clone') as HTMLElement;
96
                        clone.classList.remove('centered');
97
98
                        setTimeout(() => {
99
                            wrapper!.remove();
100
                        }, 700);
101
                    }
102
                }
103
            });
104
        }
105
106
        protected scrollClose(target: HTMLImageElement) {
107
            document.addEventListener('scroll', function () {
108
                let targetPositionTop = target.getBoundingClientRect().top;
109
                let wrapper = document.querySelector('.lightbox-clone-wrapper');
110
111
                if(wrapper) {
112
                    let clone = wrapper.querySelector('.lightbox-clone') as HTMLElement;
113
                    clone.classList.remove('centered');
114
                    clone.style.top = targetPositionTop + 'px';
115
116
                    setTimeout(() => {
117
                        wrapper!.remove();
118
                    }, 750);
119
                }
120
            })
121
        }
122
    }
123
124
    return Lightbox;
125
}));